package handler

import (
	"alipay/api/handler/request"
	"alipay/internal/dao"
	"alipay/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateOrder(c *gin.Context) {
	var req request.CreateOrder
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusBadRequest,
			"message": "验证失败",
			"data":    err.Error(),
		})
		return
	}
	var order model.Order
	dao.GetOneByFields(&model.Order{
		TicketType: req.TicketType,
	}, &order)
	if order.Title == req.Title {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusBadRequest,
			"message": "此订单已重复",
			"data":    nil,
		})
		return
	}
	order = model.Order{
		Title:        req.Title,
		CartType:     req.CartType,
		TicketType:   req.TicketType,
		InDetailType: req.InDetailType,
		OrderType:    req.OrderType,
	}
	if !dao.Create(&order) {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusBadRequest,
			"message": "订单发布失败",
			"data":    nil,
		})
		return
	}
	if order.TicketType == 3 { //车票类型为3时，为无座状态
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusBadRequest,
			"message": "无座时，用户不可进行购票操作",
			"data":    nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "ok",
		"data":    order,
	})
}

func FindOrder(c *gin.Context) {
	var o model.Order
	if o.CartType == 1 {
		var order []model.Order
		dao.GetListByFields(&model.Order{}, &order)
		var orders []*model.Order
		for _, o := range order {
			orders = append(orders, &o)
		}
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "ok",
			"data":    orders,
		})
	} else if o.CartType == 2 {
		var order []model.Order
		dao.GetListByFields(&model.Order{}, &order)
		var orders []*model.Order
		for _, o := range order {
			orders = append(orders, &o)
		}
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "ok",
			"data":    orders,
		})
	} else {
		var order []model.Order
		dao.GetListByFields(&model.Order{}, &order)
		var orders []*model.Order
		for _, o := range order {
			orders = append(orders, &o)
		}
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "ok",
			"data":    orders,
		})
	}

}

func FindsOrder(c *gin.Context) {
	var o model.Order
	if o.InDetailType == 1 {
		var order []model.Order
		dao.GetListByFields(&model.Order{}, &order)
		var orders []*model.Order
		for _, o := range order {
			orders = append(orders, &o)
		}
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "ok",
			"data":    orders,
		})
	} else if o.InDetailType == 2 {
		var order []model.Order
		dao.GetListByFields(&model.Order{}, &order)
		var orders []*model.Order
		for _, o := range order {
			orders = append(orders, &o)
		}
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "ok",
			"data":    orders,
		})
	} else if o.InDetailType == 3 {
		var order []model.Order
		dao.GetListByFields(&model.Order{}, &order)
		var orders []*model.Order
		for _, o := range order {
			orders = append(orders, &o)
		}
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "ok",
			"data":    orders,
		})
	} else {
		var order []model.Order
		dao.GetListByFields(&model.Order{}, &order)
		var orders []*model.Order
		for _, o := range order {
			orders = append(orders, &o)
		}
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "ok",
			"data":    orders,
		})

	}
}
